﻿var Class = function (p) {
    var c = function () {
        return (arguments[0] !== null && this.initialize && typeof (this.initialize) == "function") ?
            this.initialize.apply(this, arguments) : this;
    }; c.prototype = p; return c;
};
function extend(d, s) { for (var p in s) { d[p] = s[p]; } return d; }
function apply(t, fn) { return function () { fn.apply(t, arguments); }; }

//获取元素相对于 body 的坐标
function getClientRect(elem) {
    var pos = { left: 0, top: 0 };
    var offsetParent = elem.offsetParent;
    while (offsetParent != document.body) {
        pos.left += offsetParent.offsetLeft;
        pos.top += offsetParent.offsetTop;
        offsetParent = offsetParent.offsetParent;
    }
    return pos;
}

(function () {
    Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
})();

function createFlow(cb) {
    var oX = canvasPos.x - startX,
		oY = canvasPos.y - startY,
		flow = Math.max(1,100 - tool.current.options.flow);
    function run(a, b, n) {
        var i = n / flow;
        if (n > 0) {
            for (; i > 0; i--) {
                cb(a * i, b * i);
            }
        } else {
            for (; i < 0; i++) {
                cb(a * i, b * i);
            }
        }
    };
    if (Math.abs(oX) > Math.abs(oY)) {
        run(flow, flow * (oY / oX), oX);
    } else {
        run(flow * (oX / oY), flow, oY);
    }
};

// 更新画笔
function updateBrush() {
    var ctx = canvas.brush.ctx,
        t = tool.current,
        d = t.options.diameter,
        o = t.options.opacity,
        h = t.options.hardness || 100;
        d2 = d * 2;

    canvas.brush[0].width = canvas.brush[0].height = d2;

    var sr = h / 100 * d;
    sr >= 1 && sr--;

    var r = ctx.createRadialGradient(d, d, sr, d, d, d);
    r.addColorStop(0, 'rgba(0,0,0,' + o / 100 + ')');
    r.addColorStop(0.95, 'rgba(0,0,0,0)');
    r.addColorStop(1, 'rgba(0,0,0,0)');
    ctx.fillStyle = r;
    ctx.fillRect(0, 0, d2, d2);
    fillBrush();
}

// 填充画笔
function fillBrush() {
    var ctx = canvas.brush.ctx,
        t = tool.current,
        d = t.options.diameter,
        d2 = d * 2;
    ctx.globalCompositeOperation = 'source-in';
    ctx.rect(0, 0, d2, d2);
    ctx.fillStyle = tool.fillStyle;
    ctx.fill();
    ctx.globalCompositeOperation = 'source-over';
}

function getScroll() {
    var o = {};
    if (Sys.chrome) {
        o.left = document.body.scrollLeft;
        o.top = document.body.scrollTop;
    } else {
        o.left = document.documentElement.scrollLeft;
        o.top = document.documentElement.scrollTop;
    }
    return o;
}
//更新鼠标坐标
function updatePos(e) {
    var o = getScroll();
    var o1 = getClientRect(canvas.main[0]);
    canvasPos.x = e.clientX - o1.left + o.left;
    canvasPos.y = e.clientY - o1.top + o.top;
}
//将临时画布的内容更新到主画布，并清空临时画布
function updateImage() {
    canvas.main.ctx.drawImage(canvas.temp[0], 0, 0);
    canvas.temp.ctx.clearRect(0, 0, canvas.temp[0].width, canvas.temp[0].height);
}

//清空主画布
function clearPaint() {
    var temp = canvas.main.ctx.fillStyle;
    canvas.main.ctx.fillStyle = "#fff";
    canvas.main.ctx.fillRect(0, 0, canvas.main.width(), canvas.main.height());
    canvas.main.ctx.fillStyle = temp;
}

function draw(ctx,d,x,y){
    ctx.drawImage(canvas.brush[0], canvasPos.x - d - x, canvasPos.y - d - y);
}

var cn = {
    pencil:"铅笔",
    brush:"画笔",
    eraser:"橡皮擦",
    beeline:"直线",
    rectangle:"矩形",
    rotundity:"圆形",
    diameter:"直径",
    hardness:"硬度",
    flow:"流量",
    opacity:"透明度"
};
var gui_options = {
    pencil:{ 
        build:["diameter", "opacity"],
        diameter:15,
        opacity:100
    },
    brush:{ 
        build:["diameter", "hardness", "flow", "opacity"],
        diameter:100,
        hardness:100,
        flow:100,
        opacity:100
    },
    eraser:{ 
        build:["diameter", "hardness", "flow", "opacity"],
        diameter:100,
        hardness:100,
        flow:100,
        opacity:100
    },
    beeline:{ 
        build:["diameter", "opacity"],
        diameter:100,
        opacity:100
    },
    rectangle:{ 
        build:["diameter", "opacity"],
        diameter:100,
        opacity:100
    },
    rotundity:{ 
        build:["diameter", "opacity"],
        diameter:100,
        opacity:100
    }
}

